(function() {
    document.body.ondrop = function(event) {
        event.preventDefault();
        event.stopPropagation()
    };
    var diyData = {};
    var options = {};

    function diyPhone(temp, data, opts) {
        diyData = temp;
        this.init(data, opts)
    }
	
    diyPhone.prototype = {
        init: function(data, opts) {
            new Vue({
                el: '#app',
                data: {
                    diyData: data,
                    selectedIndex: -1,
                    curItem: {},
                    opts: opts
                },
                methods: {
					//添加组件
                    onAddItem: function(temp) {
                        var data = $.extend(true, {}, diyData[temp]);
                        this.diyData.items.push(data);
                        this.onEditer(this.diyData.items.length - 1)
                    },
                    onDragItemEnd: function(_0xf7ecxa) {
                        this.onEditer(_0xf7ecxa.newIndex)
                    },
                    onEditer: function(_0xf7ecxb) {
                        this.selectedIndex = _0xf7ecxb;
                        this.curItem = this.selectedIndex === 'page' ? this.diyData.page : this.diyData.items[this.selectedIndex];
                        this.initEditor()
                    },
                    onDeleleItem: function(_0xf7ecxb) {
                        var _0xf7ecxc = this;
                        layer.confirm('确定要删除吗？', 
							function(_0xf7ecxd) {
								_0xf7ecxc.diyData.items.splice(_0xf7ecxb, 1);
								_0xf7ecxc.selectedIndex = -1;
								layer.close(_0xf7ecxd)
							})
                    },
                    onEditorSelectImage: function(_0xf7ecxe, _0xf7ecxb) {
                        $.fileLibrary({
                            type: 'image',
                            done: function(_0xf7ecxf) {
								_0xf7ecxe[_0xf7ecxb] = _0xf7ecxf[0].file_path;
                            }
                        })
                    },
                    onEditorResetColor: function(_0xf7ecx10, _0xf7ecx11, _0xf7ecx12) {
                        _0xf7ecx10[_0xf7ecx11] = _0xf7ecx12
                    },
                    onEditorDeleleData: function(_0xf7ecxb, _0xf7ecx13) {
                        if (this.diyData.items[_0xf7ecx13].data.length <= 1) {
                            layer.msg('至少保留一个', {
                                anim: 6
                            });
                            return false
                        };
                        this.diyData.items[_0xf7ecx13].data.splice(_0xf7ecxb, 1)
                    },
                    onEditorAddData: function() {
                        var _0xf7ecx14 = $.extend(true, {}, diyData[this.curItem.type].data[0]);
                        this.curItem.data.push(_0xf7ecx14)
                    },
                    initEditor: function() {
                        this.$nextTick(function() {
                            if (options.hasOwnProperty('key')) {
                                options.destroy()
                            };
                            this.editorHtmlComponent();
                            if (this.curItem.type === 'richText') {
                                this.onRichText(this.curItem)
                            };
                            if (this.curItem.type === 'goods' || this.curItem.type === 'sharingGoods') {
                                this.editorSelectGoods(this.curItem)
                            }
                        })
                    },
                    editorHtmlComponent: function() {
                        var diyeditor = $(this.$refs['diy-editor']); //存在不同
                        diyeditor.find('input[type=checkbox], input[type=radio]').uCheck();
                    },
                    onSelectGoods: function(_0xf7ecx16) { //函数存在不同
                        var _0xf7ecx17 = {
                            goods: 'goods/lists&status=10',
                            sharingGoods: 'sharing.goods/lists&status=10',
                            bargainGoods: 'bargain.goods/lists&status=10'
                        };
                        $.selectData({
                            title: '选择商品',
                            uri: _0xf7ecx17[_0xf7ecx16.type],
                            duplicate: false,
                            dataIndex: goods_id,
                            done: function(data) {
                                data.forEach(function(_0xf7ecx18) {
                                    _0xf7ecx16.data.push(_0xf7ecx18)
                                })
                            },
                            getExistData: function() {
                                var _0xf7ecx19 = [];
                                _0xf7ecx16.data.forEach(function(_0xf7ecx1a) {
                                    if (_0xf7ecx1a.hasOwnProperty(goods_id)) {
                                        _0xf7ecx19.push(_0xf7ecx1a.goods_id)
                                    }
                                });
                                return _0xf7ecx19
                            }
                        })
                    },
                    onSelectShop: function(_0xf7ecx16) {
                        $.selectData({
                            title: '选择门店',
                            uri: 'shop/lists&status=1',
                            duplicate: false,
                            dataIndex: shop_id,
                            done: function(data) {
                                data.forEach(function(_0xf7ecx18) {
                                    _0xf7ecx16.data.push(_0xf7ecx18)
                                })
                            },
                            getExistData: function() {
                                var _0xf7ecx19 = [];
                                _0xf7ecx16.data.forEach(function(_0xf7ecx1b) {
                                    if (_0xf7ecx1b.hasOwnProperty(shop_id)) {
                                        _0xf7ecx19.push(_0xf7ecx1b.shop_id)
                                    }
                                });
                                return _0xf7ecx19
                            }
                        })
                    },
                    onRichText: function(_0xf7ecx16) {
                        options = UM.getEditor('ume-editor', {
                            initialFrameWidth: 375,
                            initialFrameHeight: 400
                        });
                        options.ready(function() {
                            options.setContent(_0xf7ecx16.params.content);
                            options.addListener('contentChange', function() {
                                _0xf7ecx16.params.content = options.getContent()
                            })
                        })
                    },
                    onSubmit: function() {
                        if (this.diyData.items.length <= 0) {
                            layer.msg('至少存在一个组件', {
                                anim: 6
                            });
                            return false
                        };
                        $.post('', {
                            //data: JSON.stringify(this.diyData)
							data: this.diyData
                        }, function(_0xf7ecx1c) {
                            _0xf7ecx1c.code === 1 ? $.show_success(_0xf7ecx1c.msg, _0xf7ecx1c.url) : $.show_error(_0xf7ecx1c.msg)
                        })
                    }
                }
            })
        }
    };
    window.diyPhone = diyPhone
})(window)